package com.mo.lib.ui.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dylanc.longan.activity
import com.dylanc.longan.contentView
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener
import com.mo.lib.R
import com.mo.lib.databinding.BaseListLayoutBinding
import com.mo.lib.utils.setClick
import com.mo.lib.utils.showToast

/**
 * @ author：mo
 * @ data：2021/8/27:15:46
 * @ 功能：
 */
abstract class BaseRvActivity<T> : BaseActivity(R.layout.base_list_layout) {
    protected var mPage = 1
    protected var adapter: BaseQuickAdapter<T, BaseViewHolder>? = null
    lateinit var viewBinding:BaseListLayoutBinding
    /** 配合vp使用较多，所以默认vp模式*/
    open fun isVpParent() = true

    /** 是否自动刷新   默认false*/
    open fun isAutoRefresh() = false

    /** 是否支持上滑下拉   默认false*/
    open fun isRefresh() = false

    open fun getDiffUtil(): DiffUtil.ItemCallback<T>? = null
    override fun setContentView(view: View?) {
        super.setContentView(view)
    }
    override fun initView(savedInstanceState: Bundle?) {
        viewBinding= DataBindingUtil.bind<BaseListLayoutBinding>(contentView)!!
        viewBinding.ptrlBaseList.setCanRefresh(isRefresh())
        viewBinding.ptrlBaseList.setCanLoadMore(isRefresh())
        viewBinding.ptrlBaseList.setRefreshListener(object : BaseRefreshListener {
            override fun refresh() {
                getList(1.also { mPage = it })
            }

            override fun loadMore() {
                getMore(mPage++)
            }
        })
        adapter = creatAdaper()
        getDiffUtil()?.let {
            adapter?.setDiffCallback(it)
        }
        viewBinding.krvBaseList.adapter = adapter
    }

    abstract fun creatAdaper(): BaseQuickAdapter<T, BaseViewHolder>

    /**
     * 需要自定义点击事件的时候，复写此方法
     */
    open fun emptyViewClick(view: View?) {
        getList(1.also { mPage = it })
    }

    open fun getEmptyView():View {
        val emptyView = LayoutInflater.from(activity).inflate(getEmptyLayout(), viewBinding.krvBaseList, false)
        emptyViewInit(emptyView,emptyView.findViewById<ImageView>(R.id.iv_base_empty),emptyView.findViewById<TextView>(R.id.tv_base_empty))
        emptyView.setClick {
            emptyViewClick(this)
        }
        return emptyView
    }

    open fun emptyViewInit(emptyView: View, iv_empty: ImageView?, tv_empty: TextView?) {

    }
    open fun getEmptyLayout() = R.layout.base_empty

    /**
     * 刷新数据
     */
    open fun loadList(list: MutableList<T>?) {
        if (list.isNullOrEmpty()) {
            adapter?.setEmptyView(getEmptyView())
            getDiffUtil()?.let {
                adapter?.setDiffNewData(null)
            } ?: let {
                adapter?.setNewInstance(null)
            }
        } else {
            getDiffUtil()?.let {
                adapter?.setDiffNewData(list)
            } ?: let {
                adapter?.setList(list)
            }
            mPage++
            if (isRefresh()) {
                viewBinding.ptrlBaseList.setCanLoadMore(isRefresh())
            }
        }
        viewBinding.ptrlBaseList.finishRefresh()
    }

    /**
     * 加载更多
     */
    open fun loardMore(list: MutableList<T>?) {
        if (list.isNullOrEmpty()) {
            mPage -= 1
            showToast("没有更多数据")
            viewBinding.ptrlBaseList.setCanLoadMore(false)
        } else {
            getDiffUtil()?.let {
                list.addAll(0, adapter?.data!!)
                adapter?.setDiffNewData(list)
            } ?: let {
                adapter?.addData(list)
            }
        }
        viewBinding.ptrlBaseList.finishLoadMore()
    }
    open fun setRefresh() {
        getList(1.also { mPage = it })
    }
    protected abstract fun getList(page: Int)
    protected abstract fun getMore(page: Int)
    override fun initData() {
        if (isAutoRefresh()) {
            viewBinding.ptrlBaseList.autoRefresh()
        }
    }

}